﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Web;
using WooCoo.Web.MVC;

namespace WooCoo.Portal.WebPortal
{
    public class AppConfig
    {        
        /// <summary>
        /// 主模块
        /// </summary>
        public static IWebModule MainModule;
        public static void RegisterAppConfigs()
        {
            //IOC Init
            AggregateCatalog catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new DirectoryCatalog(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"bin")));
            //catalog.Catalogs.Add(new DirectoryCatalog(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugin")));
            WooCoo.IoC.Container.Current = new WooCoo.IoC.MEFAdapter(new Lazy<CompositionContainer>(() => new CompositionContainer(catalog)));

            //Modules Init
            foreach (var module in WooCoo.IoC.Container.Current.GetExportedValues<IModule>())
            {
                WooCoo.RuntimeEnvironment.Modules.Add(module.ModuleName, module);
            }
            IModule mainmodule;
            if (WooCoo.RuntimeEnvironment.Modules.TryGetValue(Configuration.ConfigurationHelper.GetAppSettingOrDefault("MainModuleName"), out mainmodule)&& mainmodule is IWebModule)
            {
                MainModule = mainmodule as IWebModule;
            }
            else
            {
                throw new Exception("初始化错误：MainModule未设定");
            }
            //RuntimeEnvironment.DbContextType
        }
    }
}